package org.carl.component.job.util

import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap


/**
 * email carl.chen@bk.ru
 * @Author: 陈青松
 * @Date: 2019/1/19 3:03
 */
class MapUtil {
    companion object {
        /**
         * 将object转换为htmlmap
         */
        fun obj2Map(obj: Any): LinkedMultiValueMap<String, String> {
            val map = LinkedMultiValueMap<String, String>()
            if(obj is Map<*,*>){
                obj.forEach{
                    map[it.key?.toString()] = listOf(it.value?.toString())
                }
            }else{
                val fields = obj.javaClass.declaredFields
                var i = 0
                val len = fields.size
                while (i < len) {
                    val varName = fields[i].name
                    try {
                        val accessFlag = fields[i].isAccessible
                        fields[i].isAccessible = true
                        val o = fields[i].get(obj)
                        if (o != null) {
                            map[varName] = listOf(o.toString())
                        }
                        fields[i].isAccessible = accessFlag
                    } catch (var8: IllegalArgumentException) {
                        var8.printStackTrace()
                    } catch (var9: IllegalAccessException) {
                        var9.printStackTrace()
                    }
                    ++i
                }
            }
            return map
        }

        fun obj2MapWithNull(obj: Any): LinkedMultiValueMap<String, String> {
            val map = LinkedMultiValueMap<String, String>()
            val fields = obj.javaClass.declaredFields
            var i = 0
            val len = fields.size
            while (i < len) {
                val varName = fields[i].name
                try {
                    val accessFlag = fields[i].isAccessible
                    fields[i].isAccessible = true
                    val o = fields[i].get(obj)
                    if (o != null) {
                        map.put(varName, listOf(o.toString()))
                    } else {
                        map.put(varName, null)
                    }
                    fields[i].isAccessible = accessFlag
                } catch (var8: IllegalArgumentException) {
                    var8.printStackTrace()
                } catch (var9: IllegalAccessException) {
                    var9.printStackTrace()
                }
                ++i
            }
            return map
        }

        fun obj2MapWithString(obj: Any): MultiValueMap<String, String> {
            val map = LinkedMultiValueMap<String, String>()
            val fields = obj.javaClass.declaredFields
            var i = 0
            val len = fields.size
            while (i < len) {
                val varName = fields[i].name
                try {
                    val accessFlag = fields[i].isAccessible
                    fields[i].isAccessible = true
                    val o = fields[i].get(obj)
                    if (o != null) {
                        map.put(varName, listOf(o.toString()))
                    } else {
                        map.put(varName, listOf(""))
                    }
                    fields[i].isAccessible = accessFlag
                } catch (var8: IllegalArgumentException) {
                    var8.printStackTrace()
                } catch (var9: IllegalAccessException) {
                    var9.printStackTrace()
                }
                ++i
            }
            return map
        }
    }
}